/*
 * 
 * 
 * 
 */
package com.ruoyi.project.pay.qixing;

import java.io.IOException;
import java.io.Writer;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.Assert;

import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
/**
 * Utils - JSON
 * 
 * 
 * 
 */
public final class JsonUtils {

	/** ObjectMapper */
	private static ObjectMapper mapper = new ObjectMapper();
	private static final Logger logger = LoggerFactory.getLogger(JsonUtils.class);
	
	static{
		mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
	}

	/**
	 * 不可实例化
	 */
	private JsonUtils() {
	}

	/**
	 * 将对象转换为JSON字符串
	 * 
	 * @param value
	 *            对象
	 * @return JSOn字符串
	 */
	public static <T> String toJson(T value) {
		try {
			return mapper.writeValueAsString(value);
		} catch (Exception e) {
			logger.error(e.getMessage());
		}
		return null;
	}
	
	
	/**
	 * 将json转换为List集合对象
	 * 
	 * @param <T>
	 * @param json
	 * @param array
	 * @return
	 * @throws Exception
	 */
	public static <T> List<T> toCollection(String json, Class<T> clazz)
			throws Exception {
		ObjectMapper mapper = new ObjectMapper();
		List<T> objs = mapper.readValue(json,getCollectionType(List.class, clazz));
		return objs;
	}
	
	/**
	 * 将JSON字符串转换为对象
	 * 
	 * @param json
	 *            JSON字符串
	 * @param valueType
	 *            对象类型
	 * @return 对象
	 */
	public static <T> T toObject(String json, Class<T> valueType) {
		Assert.hasText(json);
		Assert.notNull(valueType);
		try {
			return mapper.readValue(json, valueType);
		} catch (Exception e) {
			logger.error(e.getMessage());
		}
		return null;
	}

	/**
	 * 将JSON字符串转换为对象
	 * 
	 * @param json
	 *            JSON字符串
	 * @param typeReference
	 *            对象类型
	 * @return 对象
	 */
	public static <T> T toObject(String json, TypeReference<?> typeReference) {
		Assert.hasText(json);
		Assert.notNull(typeReference);
		try {
			return mapper.readValue(json, typeReference);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		return null;
	}

	/**
	 * 将JSON字符串转换为对象
	 * 
	 * @param json
	 *            JSON字符串
	 * @param javaType
	 *            对象类型
	 * @return 对象
	 */
	public static <T> T toObject(String json, JavaType javaType) {
		Assert.hasText(json);
		Assert.notNull(javaType);
		try {
			return mapper.readValue(json, javaType);
		} catch (Exception e) {
			logger.error(e.getMessage());
		}
		return null;
	}

	public static JavaType getCollectionType(Class<?> collectionClass, Class<?>... elementClasses) {
		return mapper.getTypeFactory().constructParametricType(collectionClass, elementClasses);   
	} 
	
	/**
	 * 将对象转换为JSON流
	 * 
	 * @param writer
	 *            writer
	 * @param value
	 *            对象
	 */
	public static void writeValue(Writer writer, Object value) {
		try {
			mapper.writeValue(writer, value);
		} catch (JsonGenerationException e) {
			logger.error(e.getMessage());
		} catch (JsonMappingException e) {
			logger.error(e.getMessage());
		} catch (IOException e) {
			logger.error(e.getMessage());
		}
	}

	/**
	 * 返回json数据响应
	 * 
	 * @param response
	 * @param object
	 *            数据对象
	 * @param clazz
	 *            数据类型
	 * @param properties
	 *            返回属性,可为空
	 */
//	public static void responseJson(HttpServletResponse response, Object object) {
//		responseJson(response, object, null);
//	}

	/**
	 * 返回json数据响应
	 * 
	 *            返回属性,可为空
	 */
//	public static void responseJson(HttpServletResponse response,
//			Object object, Class<?> clazz, String... properties) {
//		response.setContentType("application/json; charset=UTF-8");
//		PrintWriter out = null;
//		try {
//			out = response.getWriter();
//			SimplePropertyPreFilter filter = null;
//			if (clazz != null && properties != null) {
//				filter = new SimplePropertyPreFilter(clazz, properties);
//			}
//			out.write(JSON.toJSONString(object, filter));
//		} catch (IOException e1) {
//			logger.error(e1.getMessage());
//		} finally {
//			if (out != null) {
//				out.close();
//			}
//		}
//	}

//	public static String map2Json(Map map) {
//		// 转换null
//		Iterator it = map.keySet().iterator();
//		while (it.hasNext()) {
//			String key = (String) it.next();
//			Object value = map.get(key);
//			if (value == null) {
//				map.put(key, "");
//			}
//		}
//
//		JSONObject j = JSONObject.fromObject(map);
//		return j.toString();
//	}
//
//	public static JSONObject map2JSONObject(Map map) {
//		// 转换null
//		Iterator it = map.keySet().iterator();
//		while (it.hasNext()) {
//			String key = (String) it.next();
//			Object value = map.get(key);
//			if (value == null) {
//				map.put(key, "");
//			}
//		}
//
//		JSONObject j = JSONObject.fromObject(map);
//		return j;
//	}
	
}